'''

分析模型数据

'''
from analysis import util
import json

class MatchDataModel:
    def __init__(self,nickname,fname,pei=1,iszhong=0,jiang=0.0):
        self.nickname = nickname    # 中文显示名称
        self.fname = fname          # 标示符
        self.pei = pei              # 赔数
        self.iszhong = iszhong      # 是否命中，0未中，1命中
        self.jiang = jiang          # 奖金，赔率

# 预测数据模型
class PredictionModel:
    def __init__(self,prediction,predictionName,ps=1,possibility=0.0):
        self.prediction = prediction            # 预测选项
        self.predictionName = predictionName    # 预测选项中文名称
        self.ps = ps                            # 奖金指数
        self.possibility = possibility          # 出现可能性

    def to_json(self):
        # 将类实例的属性转换为字典
        data_dict = self.__dict__
        # 使用 json.dumps() 将字典转换为 JSON 格式字符串
        data = json.dumps(data_dict, ensure_ascii=False)
        # 字符串转换成json数据
        return json.loads(data)
'''

比赛信息

'''
class MatchModel:
    def __init__(self,match):
        self.goalLine = None
        self.match = match  # 比赛信息
        self.HADNames = {"a":"负","d":"平","h":"胜"}
        self.HHADNames = {"a": "让负", "d": "让平", "h": "让胜"}
        self.HAFUNames = {"aa": "负负", "ad": "负平", "ah": "负胜","da": "平负", "dd": "平平", "dh": "平胜","ha": "胜负", "hd": "胜平", "hh": "胜胜"}
        self.TTGNames = {"s0": "0球", "s1": "1球", "s2": "2球","s3": "3球", "s4": "4球", "s5": "5球","s6": "6球", "s7": "7+"}
        self.CRSNames = {"s00s00": "0:0", "s00s01": "0:1","s00s02": "0:2", "s00s03": "0:3",
               "s00s04": "0:4", "s00s05": "0:5",
               "s01s00": "1:0", "s01s01": "1:1",
               "s01s02": "1:2", "s01s03": "1:3",
               "s01s04": "1:4", "s01s05": "1:5",
               "s02s00": "2:0", "s02s01": "2:1",
               "s02s02": "2:2", "s02s03": "2:3",
               "s02s04": "2:4", "s02s05": "2:5",
               "s03s00": "3:0", "s03s01": "3:1",
               "s03s02": "3:2", "s03s03": "3:3",
               "s04s00": "4:0", "s04s01": "4:1","s04s02": "4:2",
               "s05s00": "5:0", "s05s01": "5:1","s05s02": "5:2",
               "s-1sd": "其他平", "s-1sa": "其他负", "s-1sh": "其他胜"}
        if self.match:
            self.oddsHistory = self.match.get("fixedBonus").get("oddsHistory")
            if self.match.get("goalLine"):
                self.sectionsNo1 = self.match.get("sectionsNo1")
                self.sectionsNo999 = self.match.get("sectionsNo999")
                self.goalLine = self.match.get("goalLine")
                self.matchResult = util.getMatchResult(self.sectionsNo1,self.sectionsNo999,self.goalLine)
                self.isSelling = False
            else:
                # 认为是正在销售的数据
                self.isSelling = True
                if self.oddsHistory and self.oddsHistory.get("hhadList"):
                    self.goalLine = self.oddsHistory.get("hhadList")[0].get("goalLine")

    def getInfo(self):
        return {
            "matchDate":self.match.get("matchDate"),
            "goalLine": self.goalLine,
            "matchNumStr": self.match.get("matchNumStr"),
            "homeTeamAbbName": self.match.get("homeTeamAbbName"),
            "awayTeamAbbName": self.match.get("awayTeamAbbName"),
            "leagueAbbName": self.match.get("leagueAbbName"),
            "matchTime": self.match.get("matchTime"),
            "matchId":self.match.get("matchId"),
        }

    # 获取胜平负信息
    def getHAD(self,tojson):
        res = []
        if self.oddsHistory and self.oddsHistory.get("hadList"):
            had = self.oddsHistory.get("hadList")[-1]
            spf = {"h": float(had.get("h")), "d": float(had.get("d")), "a": float(had.get("a"))}
            # 返回[("a",3.3,)，("h",3.5), ("d",4)....], 列表序号表示赔数
            peid_spf = util.to_pei_list(spf)
            for i in range(len(peid_spf)):
                fname = peid_spf[i][0]
                nickname = self.HADNames.get(fname)
                pei = i+1
                jiang = float(peid_spf[i][1])
                iszhong = 0     # 默认不中
                if not self.isSelling:
                    if fname == self.matchResult.get("had"):
                        iszhong = 1
                # 转换成对象
                data = MatchDataModel(nickname, fname, pei, iszhong, jiang)
                if tojson:
                    # 将类实例的属性转换为字典
                    data_dict = data.__dict__
                    # 使用 json.dumps() 将字典转换为 JSON 格式字符串
                    data = json.dumps(data_dict, ensure_ascii=False)
                    # 字符串转换成json数据
                res.append(json.loads(data))
        return res


    # 获取让球胜平负信息
    def getHHAD(self,tojson):
        res = []
        if self.oddsHistory and self.oddsHistory.get("hhadList"):
            hhad = self.oddsHistory.get("hhadList")[-1]
            spf = {"h": float(hhad.get("h")), "d": float(hhad.get("d")), "a": float(hhad.get("a"))}
            # 返回[("a",3.3,)，("h",3.5), ("d",4)....], 列表序号表示赔数
            peid_spf = util.to_pei_list(spf)
            for i in range(len(peid_spf)):
                fname = peid_spf[i][0]
                nickname = self.HHADNames.get(fname)
                pei = i+1
                jiang = float(peid_spf[i][1])
                iszhong = 0     # 默认不中
                if not self.isSelling:
                    if fname == self.matchResult.get("hhad"):
                        iszhong = 1
                # 转换成对象
                data = MatchDataModel(nickname, fname, pei, iszhong, jiang)
                if tojson:
                    # 将类实例的属性转换为字典
                    data_dict = data.__dict__
                    # 使用 json.dumps() 将字典转换为 JSON 格式字符串
                    data = json.dumps(data_dict, ensure_ascii=False)
                    # 字符串转换成json数据
                res.append(json.loads(data))
        return res

    # 获取半全场信息
    def getHAFU(self,tojson):
        res = []

        if self.oddsHistory and self.oddsHistory.get("hafuList"):

            hafu = self.oddsHistory.get("hafuList")[-1]
            spf = {"hh": float(hafu.get("hh")), "hd": float(hafu.get("hd")), "ha": float(hafu.get("ha")),
                   "dh": float(hafu.get("dh")), "dd": float(hafu.get("dd")), "da": float(hafu.get("da")),
                   "ah": float(hafu.get("ah")), "ad": float(hafu.get("ad")), "aa": float(hafu.get("aa"))}
            # 返回[("a",3.3,)，("h",3.5), ("d",4)....], 列表序号表示赔数
            peid_spf = util.to_pei_list(spf)
            for i in range(len(peid_spf)):
                fname = peid_spf[i][0]
                nickname = self.HAFUNames.get(fname)
                pei = i+1
                jiang = float(peid_spf[i][1])
                iszhong = 0     # 默认不中
                if not self.isSelling:
                    if fname == self.matchResult.get("hafu"):
                        iszhong = 1
                # 转换成对象
                data = MatchDataModel(nickname, fname, pei, iszhong, jiang)
                if tojson:
                    # 将类实例的属性转换为字典
                    data_dict = data.__dict__
                    # 使用 json.dumps() 将字典转换为 JSON 格式字符串
                    data = json.dumps(data_dict,ensure_ascii=False)
                    # 字符串转换成json数据
                res.append(json.loads(data))
        return res

    # 获取总进球信息
    def getTTG(self,tojson):
        res = []
        if self.oddsHistory and self.oddsHistory.get("ttgList"):
            ttg = self.oddsHistory.get("ttgList")[-1]
            spf = {"s0": float(ttg.get("s0")), "s1": float(ttg.get("s1")), "s2": float(ttg.get("s2")),
                   "s3": float(ttg.get("s3")), "s4": float(ttg.get("s4")), "s5": float(ttg.get("s5")),
                   "s6": float(ttg.get("s6")), "s7": float(ttg.get("s7"))}
            # 返回[("a",3.3,)，("h",3.5), ("d",4)....], 列表序号表示赔数
            peid_spf = util.to_pei_list(spf)
            for i in range(len(peid_spf)):
                fname = peid_spf[i][0]
                nickname = self.TTGNames.get(fname)
                pei = i+1
                jiang = float(peid_spf[i][1])
                iszhong = 0     # 默认不中
                if not self.isSelling:
                    if fname == self.matchResult.get("ttg"):
                        iszhong = 1
                # 转换成对象
                data = MatchDataModel(nickname, fname, pei, iszhong, jiang)
                if tojson:
                    # 将类实例的属性转换为字典
                    data_dict = data.__dict__
                    # 使用 json.dumps() 将字典转换为 JSON 格式字符串
                    data = json.dumps(data_dict, ensure_ascii=False)
                    # 字符串转换成json数据
                res.append(json.loads(data))
        return res

    # 获取比分信息
    def getCRS(self,tojson):
        res = []
        if self.oddsHistory and self.oddsHistory.get("crsList"):
            crs = self.oddsHistory.get("crsList")[-1]
            spf = {"s00s00": float(crs.get("s00s00")),"s00s01": float(crs.get("s00s01")),"s00s02": float(crs.get("s00s02")),"s00s03": float(crs.get("s00s03")),"s00s04": float(crs.get("s00s04")),"s00s05": float(crs.get("s00s05")),
                   "s01s00": float(crs.get("s01s00")),"s01s01": float(crs.get("s01s01")),"s01s02": float(crs.get("s01s02")),"s01s03": float(crs.get("s01s03")),"s01s04": float(crs.get("s01s04")),"s01s05": float(crs.get("s01s05")),
                   "s02s00": float(crs.get("s02s00")),"s02s01": float(crs.get("s02s01")),"s02s02": float(crs.get("s02s02")),"s02s03": float(crs.get("s02s03")),"s02s04": float(crs.get("s02s04")),"s02s05": float(crs.get("s02s05")),
                   "s03s00": float(crs.get("s03s00")),"s03s01": float(crs.get("s03s01")),"s03s02": float(crs.get("s03s02")),"s03s03": float(crs.get("s03s03")),
                   "s04s00": float(crs.get("s04s00")),"s04s01": float(crs.get("s04s01")),"s04s02": float(crs.get("s04s02")),
                   "s05s00": float(crs.get("s05s00")),"s05s01": float(crs.get("s05s01")),"s05s02": float(crs.get("s05s02")),
                   "s-1sd": float(crs.get("s-1sd")),"s-1sa": float(crs.get("s-1sa")),"s-1sh": float(crs.get("s-1sh"))}
            # 返回[(3.3, "a")，(3.5, "h"), (4,"d")....], 列表序号表示赔数
            peid_spf = util.to_pei_list(spf)
            for i in range(len(peid_spf)):
                fname = peid_spf[i][0]
                nickname = self.CRSNames.get(fname)
                pei = i+1
                jiang = float(peid_spf[i][1])
                iszhong = 0     # 默认不中
                if not self.isSelling:
                    if fname == self.matchResult.get("crs"):
                        iszhong = 1
                # 转换成对象
                data = MatchDataModel(nickname, fname, pei, iszhong, jiang)
                if tojson:
                    # 将类实例的属性转换为字典
                    data_dict = data.__dict__
                    # 使用 json.dumps() 将字典转换为 JSON 格式字符串
                    data = json.dumps(data_dict, ensure_ascii=False)
                    # 字符串转换成json数据
                res.append(json.loads(data))
        return res

    # 分析
    def getANA(self,ds):
        res = {}
        # 分析胜平负
        res["HAD"] = self.getAnaHAD(ds)
        # 分析让球胜平负
        res["HHAD"] = self.getAnaHHAD(ds)
        # 分析比分
        res["CRS"] = self.getAnaCRS(ds)
        # 分析半全场
        res["HAFU"] = self.getAnaHAFU(ds)
        # 分析总进球
        res["TTG"] = self.getAnaTTG(ds)
        return res

    # 分析胜平负
    def getAnaHAD(self, ds):
        res = []
        # 开始分析
        # 找出相类似的数据
        
        res.append(PredictionModel("a", "负", 1.2, 0.9).to_json())
        res.append(PredictionModel("d", "平", 3.2, 0.95).to_json())
        return res
    # 分析让球胜平负
    def getAnaHHAD(self, ds):
        res = []
        res.append(PredictionModel("d", "让平", 3.2, 0.95).to_json())
        res.append(PredictionModel("d", "让负", 3.2, 0.95).to_json())
        return res
    # 分析比分
    def getAnaCRS(self, ds):
        res = []
        res.append(PredictionModel("d", "2:3", 3.2, 0.95).to_json())
        return res
    # 分析半全场
    def getAnaHAFU(self, ds):
        res = []
        res.append(PredictionModel("d", "平胜", 3.2, 0.95).to_json())
        return res
    # 分析总进球
    def getAnaTTG(self, ds):
        res = []
        res.append(PredictionModel("d", "7+", 3.2, 0.95).to_json())
        return res